package cn.edu.nwafu.hjpg.controller;

import cn.edu.nwafu.hjpg.common.HttpResult;
import cn.edu.nwafu.hjpg.entity.*;

import cn.edu.nwafu.hjpg.service.HjxwbService;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/hjxwb")
public class HjxwbController {
    final HjxwbService hjxwbService;
    //private boolean required;

    public HjxwbController(HjxwbService hjxwbService) {
        this.hjxwbService = hjxwbService;
    }

    @GetMapping(value = "/")
    @ResponseBody
    public HttpResult get(@RequestParam(value = "labelName",defaultValue = "",required = false) String name,
                          @RequestParam(value = "elementId",defaultValue = "",required = false) String id,
                          @RequestParam(value = "flag",defaultValue = "",required = false) String flag) {
        return HttpResult.ok(hjxwbService.findByIdAndFlagInHjxwb(id,name,flag));
    }
    @PostMapping(value = "/update")
    @ResponseBody
    public HttpResult update(@RequestBody Hjxwb hjxwb) {
        return HttpResult.ok(hjxwbService.updatehjxwb(hjxwb));
    }

    @PostMapping(value = "/add")
    @ResponseBody
    public HttpResult add(@RequestBody(required = false) Hjxwb hjxwb,@RequestParam(value = "labelName",defaultValue = "",required = false) String name) {
        switch (name) {
            case "环境行为概述":
                return HttpResult.ok(hjxwbService.addHjxwgs(hjxwb));
            case "生物降解半衰期":
                return HttpResult.ok(hjxwbService.addSwjjbsq(hjxwb));
            case "生物降解":
                return HttpResult.ok(hjxwbService.addSwjj(hjxwb));
            case "光解半衰期":
                return HttpResult.ok(hjxwbService.addGjbsq(hjxwb));
            case "水解半衰期":
                return HttpResult.ok(hjxwbService.addSjbsq(hjxwb));
            case "其他降解性说明":
                return HttpResult.ok(hjxwbService.addQtjjxsm(hjxwb));
            case "BCF值":
                return HttpResult.ok(hjxwbService.addBcf(hjxwb));
            case "BMF值":
                return HttpResult.ok(hjxwbService.addBmf(hjxwb));
            case "TMF值":
                return HttpResult.ok(hjxwbService.addTmf(hjxwb));
            case "BASF值":
                return HttpResult.ok(hjxwbService.addBasf(hjxwb));
            case "生物积累性备注":
                return HttpResult.ok(hjxwbService.addSwljxbz(hjxwb));
            case "其他环境行为信息":
                return HttpResult.ok(hjxwbService.addQthjxwxx(hjxwb));
            default:
                return null;
        }
    }
    @GetMapping(value = "/addRecord")
    @ResponseBody
    public HttpResult queryhjxwbByPage(@RequestParam(value = "flag", defaultValue = "") String flag,
                                       @RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                       @RequestParam(value = "pageSize", defaultValue = "20") int pageSize){

            Pageable pageable = PageRequest.of(pageNumber - 1, pageSize);
            return HttpResult.ok(hjxwbService.findAddRecordHjxwb(flag,pageable));
        }
    @GetMapping(value = "/find")
    @ResponseBody
    public HttpResult findhjxwbByPage(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                      @RequestParam(value = "pageSize", defaultValue = "7") int pageSize,
                                      @RequestParam(value = "attribute", defaultValue = "") String attribute,
                                      @RequestParam(value = "flag", defaultValue = "1") int flag,
                                      @RequestParam(value = "id",defaultValue = "",required = false) String id){

        Pageable pageable = PageRequest.of(pageNumber - 1, pageSize);
        return HttpResult.ok(hjxwbService.findByPageHjxwb(id,attribute,flag,pageable));
    }
    @GetMapping(value = "/export")
    @ResponseBody
    public HttpResult get() {
        return HttpResult.ok(hjxwbService.findAll());
    }

    @PostMapping(value = "/addupdateFlag")
    @ResponseBody
    public HttpResult addupdateFlag(@RequestBody Hjxwb hjxwb) {
            System.out.println(hjxwb.toString());
            return HttpResult.ok(hjxwbService.addupdateFlag(hjxwb));
        }

    @DeleteMapping("/{id}")
    @ResponseBody
    public HttpResult deladdRecordhjxwb(Hjxwb hjxwb){
            hjxwbService.deladdRecordhjxwb(hjxwb);
            return HttpResult.ok();
        }
    @PostMapping(value = "/import")
    @ResponseBody
    public HttpResult addExcelInHjxwb(@RequestBody List<Hjxwb> hjxwbList) {
        for (int i = 0 ; i <hjxwbList.size() ; i ++ ){
            hjxwbList.get(i).setFlag("1");
            hjxwbService.addElement(hjxwbList.get(i));
        }
        return HttpResult.ok();
    }


    @GetMapping(value = "/all")
    @ResponseBody
    public HttpResult findAll(@RequestParam(value = "elementId",defaultValue = "",required = false) String id,
                              @RequestParam(value = "flag",defaultValue = "",required = false) String flag) {
        System.out.println("**********************");
        return HttpResult.ok(hjxwbService.findAllByBaseinfoWzbmAndFlag(id,flag));
    }

    }

